package com.tanhua.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.model.domain.Analysis;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.vo.SummaryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author kcs
 * @date 2022/1/8
 * @TIME:17:10
 */
@Service
public class DashboardService {

    @Autowired
    private AnalysisMapper analysisMapper;

    @DubboReference
    private UserApi userApi;

    @Autowired
    private LogMapper logMapper;

    //新增、活跃用户、次日留存率
    public Map users(String sd, String ed, String type) {

        //1.查询今年数据
        String start = getDate(Long.valueOf(sd));
        String end = getDate(Long.valueOf(ed));
        List<Map> thisYear = getYearMap(type, start, end);

        //2.查询去年数据
        String start1 = getDate(Long.valueOf(sd) - 31536000000l);
        String end1 = getDate(Long.valueOf(ed) - 31536000000l);
        List<Map> lastYear = getYearMap(type, start1, end1);

        //3.封装返回
        Map<String, List> map = new HashMap<>();
        map.put("thisYear", thisYear);
        map.put("lastYear", lastYear);
        return map;
    }

    //新增、活跃用户、次日留存率 -- 公共抽取方法
    private List<Map> getYearMap(String type, String start, String end) {

        QueryWrapper<Analysis> wrapper = new QueryWrapper<>();
        wrapper.between("record_date", start, end);
        List<Analysis> analyses = analysisMapper.selectList(wrapper);

        List<Map> year = new ArrayList<>();
        int i = 1;
        if (type.equals("101")) {
            for (Analysis analysis : analyses) {
                Map map = new HashMap();
                String title = (i++) + "";
                Integer amount = analysis.getNumRegistered();
                map.put("title", title);
                map.put("amount", amount);
                year.add(map);
            }
        }
        if (type.equals("102")) {
            for (Analysis analysis : analyses) {
                Map map = new HashMap();
                String title = (i++) + "";
                Integer amount = analysis.getNumActive();
                map.put("title", title);
                map.put("amount", amount);
                year.add(map);
            }
        }
        if (type.equals("103")) {
            for (Analysis analysis : analyses) {
                Map map = new HashMap();
                String title = (i++) + "";
                Integer amount = analysis.getNumRetention1d();
                map.put("title", title);
                map.put("amount", amount);
                year.add(map);
            }
        }
        return year;
    }

    //新增、活跃用户、次日留存率 -- 转换时间类型 : 毫秒值 --> 日期字符串
    public String getDate(long milliSecond) {
        Date date = new Date();
        date.setTime(milliSecond);
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }


    public SummaryVo summary() {
        SummaryVo summaryVo = new SummaryVo();
        //获取累计用户
        Integer cumulativeUsers = userApi.count();
        summaryVo.setCumulativeUsers(cumulativeUsers);
        //获取过去30天内活跃用户
        Integer activePassMonth = logMapper.activePassMonthOrWeek(30);
        summaryVo.setActivePassMonth(activePassMonth);
        //获取过去7天内活跃用户
        Integer activePassWeek = logMapper.activePassMonthOrWeek(7);
        summaryVo.setActivePassWeek(activePassWeek);
        //获取今日时间 并转换为指定格式
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String yesterFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis() - 86400000));
        //今日新增用户
        Integer newUsersToday = userApi.addtoday();
        summaryVo.setNewUsersToday(newUsersToday);
        //昨日新增用户
        Integer newUsersYesterDay = userApi.addYesterDay();
        //计算今日新增用户相较昨日环比  （本期数－同期数）÷同期数×100%
        Integer newUsersTodayRate = 0;
        if (newUsersToday != 0 && newUsersYesterDay != 0) {
            newUsersTodayRate = ((newUsersToday - newUsersYesterDay) / newUsersYesterDay) * 100;
        }
        summaryVo.setNewUsersTodayRate(newUsersTodayRate);

        //今日登录次数
        //0101代表用户登录时日志显示的类型 format 为当前日期(精确到日)
        Integer loginTimesToday = logMapper.loginTimesToday("0101", format);
        summaryVo.setLoginTimesToday(loginTimesToday);
        //获取昨日登录次数
        Integer loginTimesYester = logMapper.loginTimesToday("0101", yesterFormat);
        //计算昨日登录用户相较于今日环比
        Integer loginTimesTodayRate = 0;
        if (loginTimesToday != 0 && loginTimesYester != 0) {
            loginTimesTodayRate = ((loginTimesToday - loginTimesYester) / loginTimesYester) * 100;
        }
        summaryVo.setLoginTimesTodayRate(loginTimesTodayRate);

        //查询今日活跃用户
        Integer activeUsersToday = logMapper.activeUsersToday(format);
        summaryVo.setActiveUsersToday(activeUsersToday);
//        查询昨日活跃用户
        Integer activeUsersYester = logMapper.activeUsersToday(yesterFormat);
        //计算昨日活跃用户相较于今日
        Integer activeUsersTodayRate = 0;
        if (activeUsersToday != 0 && activeUsersYester != 0) {
            activeUsersTodayRate = ((activeUsersToday - activeUsersYester) / activeUsersYester) * 100;
        }

        summaryVo.setActiveUsersTodayRate(activeUsersTodayRate);
        return summaryVo;

    }
}
